//
//  DetailViewController.m
//  AutoHome
//
//  Created by Flying meat on 9/15/15.
//  Copyright (c) 2015 CaoFeng. All rights reserved.
//

#import "DetailViewController.h"
#import "RecommendViewController.h"
#import "TabBarViewController.h"
#import "UMSocial.h"
#import "DetailCell.h"
#define Url_newsinfo @"http://cont.app.autohome.com.cn/autov4.8.8/content/news/newsinfo-pm1-i%d.json"
#define Url_videopage @"http://cont.app.autohome.com.cn/autov4.8.8/content/news/videopage-a2-pm1-v4.8.9-vid%d-night0-showpage1-fs0-cw320-rct1.json"
#define Url_shuoke @"http://app.api.autohome.com.cn/autov4.8.8/news/shuokeinfo-pm1-n%d.json"
#define Url_fastnew @"http://app.api.autohome.com.cn/autov4.8.8/news/fastnewseries-pm1-n%d.json"

#define Url_newscontent @"http://cont.app.autohome.com.cn/autov4.8.8/content/news/newscontent-n%d-t1-rct1.json"
@interface DetailViewController ()<UMSocialUIDelegate>
{
    int _i;
    UIWebView *webView;
    NSString *_url;
}
@property (weak, nonatomic) IBOutlet UIButton *collectButton;
@end

@implementation DetailViewController
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _url=nil;
        _i=-1;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
   self.newcount=[self.EveryID intValue];
    [self setWebView];
   

}


-(void)setWebView
{
    
    webView=[[UIWebView alloc]initWithFrame:CGRectMake(0,64 , self.view.frame.size.width, self.view.frame.size.height-100)];
    
    if (self.newcount>30000&&self.newcount<35000)
    {
        
        
        
        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:Url_shuoke,self.newcount]]];
        
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            if (connectionError)
            {
                NSLog(@"error=%@",connectionError);
            }
            else
            {
                
                NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                
                
                NSDictionary *dicnew=dic[@"result"];
                _url=dicnew[@"weburl"];
                NSURLRequest *request2=[NSURLRequest requestWithURL:[NSURL URLWithString:_url]];
                
                
                [self.view addSubview:webView];
                [webView loadRequest:request2];
                
            }
        }];
        
    }
    else if (self.newcount>35000&&self.newcount<800000)
    {
        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:Url_videopage,self.newcount]]];
        
       
        _url=[NSString stringWithFormat:Url_videopage,self.newcount];
        [self.view addSubview:webView];
        [webView loadRequest:request];
        
        
        
    }
    else if (self.newcount<500)
    {
        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:Url_fastnew,self.newcount]]];
        
        _url=[NSString stringWithFormat:Url_fastnew,self.newcount];
        
                
                [self.view addSubview:webView];
                [webView loadRequest:request];
       
    }
    else if(self.newcount>38820000)
    {
        
        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:Url_newscontent,self.newcount]]];
        _url=[NSString stringWithFormat:Url_newscontent,self.newcount];
        
        
        [self.view addSubview:webView];
        [webView loadRequest:request];
    }
    //=============论坛精选日报数据接口============还没做
    else
    {
        
        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:Url_newsinfo,self.newcount]]];
        [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
        {
            if (connectionError) {
                NSLog(@"error=%@",connectionError);
            }
            else
            {
                
                NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                
                
                NSDictionary *dicnew=dic[@"result"];
                _url=dicnew[@"url"];
                
                
                NSURLRequest *request2=[NSURLRequest requestWithURL:[NSURL URLWithString:_url]];
                
                
                [self.view addSubview:webView];
                [webView loadRequest:request2];

            }
        }];
        
    }
    
    


    
    
    
}
- (IBAction)collectButton:(id)sender {
    
   //先查缓存是否有其网址,如果有就,设置button的图片为黄色五角星;
    //如果没有就设置button为灰色五角星;
    
    if(_i*1>0)
    {
        [self.collectButton setImage:[UIImage imageNamed:@"btn_icon_collect.png"] forState:UIControlStateNormal];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"已取消" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        _i=1;
    }
    else
    {
    
    [self.collectButton setImage:[UIImage imageNamed:@"btn_icon_collected_1.png"] forState:UIControlStateNormal];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"已收藏" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
        }
    _i=_i*-1;
}

- (IBAction)back:(id)sender {
    

 
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (IBAction)share:(id)sender {
    [UMSocialSnsService presentSnsIconSheetView:self appKey:nil shareText:_url shareImage:[UIImage imageNamed:@"spring_33.png"] shareToSnsNames:@[UMShareToSina,UMShareToRenren,UMShareToTencent,UMShareToTwitter,UMShareToWechatTimeline,UMShareToWechatSession] delegate:self];
}
#pragma  mark -UMSocialUIDelegate
//分享结束代理
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    
    if (response.responseCode == UMSResponseCodeSuccess)
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"分享成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"分享失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        NSLog(@"分享失败,错误==%@",response.message);
        
    }
    
    
    
}

//- (IBAction)login:(id)sender {
//    
//    UMSocialSnsPlatform *snsPlatfrom=[UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
//    //登陆操作
//    snsPlatfrom.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
//        
//        if (response.responseCode==UMSResponseCodeSuccess) {
//            NSLog(@"第三方登陆成功");
//            
//            //登录成成功后,可以获取用户信息
//            [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina completion:^(UMSocialResponseEntity *response) {
//                NSLog(@"data===%@",response.data);
//                NSLog(@"location==%@",response.data[@"location"]);
//                NSLog(@"name==%@",response.data[@"screen_name"]);
//            }];
//            //获取好友信息
//            [[UMSocialDataService defaultDataService]requestSnsFriends:UMShareToSina completion:^(UMSocialResponseEntity *response) {
//                NSLog(@"好友===%@",response.data);
//                NSDictionary *dic=response.data;
//                //获取字典中所有的values;
//                NSArray *array=dic.allValues;
//                for (NSDictionary *friend in array) {
//                    NSLog(@"名字==%@",friend[@"name"]);
//                }
//            }];
//            
//            
//            
//        }else{
//            
//            NSLog(@"第三方登陆失败,错误==%@",response.message);
//        }
//        
//        
//        
//    });
//    
//    
//    
//}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
